<?php

namespace App\HttpController\Admin;

use App\Model\ShopsModel;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;

class ShopController extends AuthController {

    public function index(){
        $request= $this->request();
        $params   = $request->getRequestParam();
        MysqlPool::invoke(function (MysqlObject $db) use($params){
            $model  = new ShopsModel($db);
            $list   = $model->where(function ($query) use($params){
                    if($params['kwd']){
                        $query->where('name','%'.$params['kwd'].'%','like')
                            ->whereOr('mac','%'.$params['kwd'].'%','like');
                    }
                })
                ->orderBy('created_at')
                ->paginate($params['page']);
            $this->stateToText($list['data'],[
                'status' => $model::STATUS_TEXT,
                'online' => $model::ONLINE_TEXT
            ]);
            return $this->writeJson(0,$list);
        });
    }

    public function all(){
        MysqlPool::invoke(function (MysqlObject $db){
            $model = new ShopsModel($db);
            $list  = $model->select('id,name')->where('status',$model::STATUS_1)->orderBy('created_at')->get();
            return $this->writeJson(0,$list);
        });
    }


    public function update(){
        $request = $this->request();
        $data = $request->getRequestParam();
        MysqlPool::invoke(function (MysqlObject $db) use($data){
            $model  = new ShopsModel($db);
            if($data['id']){
                $model->update($data);
                return $this->writeJson(0,null,'编辑店铺信息成功');
            }else{
                //新增
                $data['created_at'] = date('Y-m-d H:i:s');
                $id = $model->create($data);
                return $this->writeJson(0,null,'新增店铺信息成功');
            }
        });
    }

    public function open(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new ShopsModel($db);
            $model->update(['id'=>$id,'status'=>$model::STATUS_1]);
            return $this->writeJson(0);
        });
    }

    public function close(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new ShopsModel($db);
            $model->update(['id'=>$id,'status'=>$model::STATUS_2]);
            return $this->writeJson(0);
        });
    }


    public function destroy(){
        $request = $this->request();
        $id = (int)$request->getRequestParam('id');
        if(empty($id)) return $this->writeJson(1,null,'参数错误');
        MysqlPool::invoke(function (MysqlObject $db) use($id){
            $model  = new ShopsModel($db);
            $model->destroy($id);
            return $this->writeJson(0);
        });
    }




}
